const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
// 引入【管理员】权限认证中间件
const adminAuth = require("./middlewares/admin-auth");
// 引入【用户】权限认证中间件
const userAuth = require("./middlewares/user-auth");
// 管理配置环境变量: Node.js v20.6 可以直接使用 .env 文件配置环境变量, 无需依赖第三方模块(如 dotenv)
require("dotenv").config();
// 引入跨域
const cors = require("cors");
// 启动邮件消费者
const { mailConsumer } = require("./utils/rabbit-mq");
(async () => {
  // await mailConsumer();
  // console.log("邮件消费者已启动");
})();

// 前端接口路由文件
const indexRouter = require("./routes/index");
const categoriesRouter = require("./routes/categories");
const coursesRouter = require("./routes/courses");
const chaptersRouter = require("./routes/chapters");
const articlesRouter = require("./routes/articles");
const settingsRouter = require("./routes/settings");
const searchRouter = require("./routes/search");
const authRouter = require("./routes/auth");
const usersRouter = require("./routes/users");
const likesRouter = require("./routes/likes");
const membershipsRouter = require("./routes/memberships");
const uploadsRouter = require("./routes/uploads");
const captchaRouter = require("./routes/captcha");

// 后台接口路由文件
const adminArticlesRouter = require("./routes/admin/articles");
const adminCategoriesRouter = require("./routes/admin/categories");
const adminSettingsRouter = require("./routes/admin/settings");
const adminUsersRouter = require("./routes/admin/users");
const adminCoursesRouter = require("./routes/admin/courses");
const adminChaptersRouter = require("./routes/admin/chapters");
const adminChartsRouter = require("./routes/admin/charts");
const adminAttachmentsRouter = require("./routes/admin/attachments");
const adminLogsRouter = require("./routes/admin/logs");
const adminMembershipsRouter = require('./routes/admin/memberships');
const adminAuthRouter = require("./routes/admin/auth");

const app = express();

app.use(logger("dev"));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
// CORS 跨域配置，解决浏览器的同源策略问题
const corsOptions = {
  // 如果能明确前端项目部署后的域名，这里就写具体的域名，比如：https://example.com
  origin: ["https://example.com", "http://127.0.0.1:5500"], // 允许跨域的域名, *代表允许任意域名跨域
};
app.use(cors(corsOptions));

// 前台路由配置
app.use("/", indexRouter);
app.use("/categories", categoriesRouter);
app.use("/courses", coursesRouter);
app.use("/chapters", userAuth, chaptersRouter);
app.use("/articles", articlesRouter);
app.use("/settings", settingsRouter);
app.use("/search", searchRouter);
app.use("/auth", authRouter);
app.use("/users", userAuth, usersRouter);
app.use("/likes", userAuth, likesRouter);
app.use("/memberships", membershipsRouter);
app.use("/uploads", userAuth, uploadsRouter);
app.use("/captcha", captchaRouter);

// 后台路由配置, 需要先通过【权限认证】的中间件, 再运行路由文件访问后台接口
app.use("/admin/articles", adminAuth, adminArticlesRouter);
app.use("/admin/categories", adminAuth, adminCategoriesRouter);
app.use("/admin/settings", adminAuth, adminSettingsRouter);
app.use("/admin/users", adminAuth, adminUsersRouter);
app.use("/admin/courses", adminAuth, adminCoursesRouter);
app.use("/admin/chapters", adminAuth, adminChaptersRouter);
app.use("/admin/charts", adminAuth, adminChartsRouter);
app.use("/admin/attachments", adminAuth, adminAttachmentsRouter);
app.use("/admin/logs", adminAuth, adminLogsRouter);
app.use('/admin/memberships', adminAuth, adminMembershipsRouter);
// 登录接口, 不需要权限认证
app.use("/admin/auth", adminAuthRouter);

module.exports = app;
